﻿using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RunWrapper = Microsoft.SqlServer.Dts.Runtime.Wrapper;
using DatabaseGroup.SSIS.SourceAdapters.View;
using DatabaseGroup.SSIS.SourceAdapters;
using DatabaseGroup.SSIS.SourceAdapters.Presenter;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using System;

namespace DatabaseGroup.IntegrationTests.SSIS.SourceAdapters
{
    [TestClass]
    public class LogOutputTest
    {
        private TestContext _testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }

        [TestMethod]
        public void LogOut_ASCII5ColTQNumericUKInput_FilesMatch()
        {
            DFSIntegrationTestPackage dfs = new DFSIntegrationTestPackage("Input_Err_ASCII_5Col_TQ_UK.txt", _testContextInstance);
            ColumnProperty col = dfs.CreateCol("HEADER1", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER2", 0, 0, RunWrapper.DataType.DT_UI1, 0, 0);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER3", 0, 0, RunWrapper.DataType.DT_I4, 0, 0);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_RedirectRow);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER4", 12, 6, RunWrapper.DataType.DT_NUMERIC, 0, 0);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER5", 0, 0, RunWrapper.DataType.DT_DBTIMESTAMP, 0, 0);
            col.ErrorHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            dfs.GeneralSettings.Locale = PresenterUtils.ConvertLocaleIDToString(2057);
            dfs.GeneralSettings.TextQualifier = "\"";
            dfs.GeneralSettings.ColumnNamesInFirstDataRow = true;

            dfs.Execute();
            Assert.IsTrue(dfs.IsStandardOutputFileSameAs("Results_StdOut_Log_ASCII_5Col_TQ_UK.txt"));
            Assert.IsTrue(dfs.IsLogOutputFileSameAs("Results_LogOut_Log_ASCII_5Col_TQ_UK.txt"));
            Assert.IsTrue(dfs.IsErrorOutputFileSameAs("Results_ErrOut_Log_ASCII_5Col_TQ_UK.txt"));
        }

        [TestMethod]
        public void LogOut_UTF85ColTQNumericUKInput_FilesMatch()
        {
            DFSIntegrationTestPackage dfs = new DFSIntegrationTestPackage("Input_Err_UTF8_5Col_TQ_UK.txt", _testContextInstance);
            ColumnProperty col = dfs.CreateCol("HEADER1", 0, 0, RunWrapper.DataType.DT_WSTR, 255, 0);
            col.ErrorHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER2", 0, 0, RunWrapper.DataType.DT_UI1, 0, 0);
            col.ErrorHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER3", 0, 0, RunWrapper.DataType.DT_I4, 0, 0);
            col.ErrorHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER4", 12, 6, RunWrapper.DataType.DT_NUMERIC, 0, 0);
            col.ErrorHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER5", 0, 0, RunWrapper.DataType.DT_DBTIMESTAMP, 0, 0);
            col.ErrorHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            dfs.GeneralSettings.Locale = PresenterUtils.ConvertLocaleIDToString(2057);
            dfs.GeneralSettings.CodePage = PresenterUtils.ConvertCodepageToString(65001);
            dfs.GeneralSettings.TextQualifier = "\"";
            dfs.GeneralSettings.ColumnNamesInFirstDataRow = true;
            dfs.AuditingSettings.ErrorSourceDataType = Enum.GetName(typeof(RunWrapper.DataType), RunWrapper.DataType.DT_WSTR);

            dfs.Execute();
            Assert.IsTrue(dfs.IsStandardOutputFileSameAs("Results_StdOut_Log_UTF8_5Col_TQ_UK.txt"));
            Assert.IsTrue(dfs.IsLogOutputFileSameAs("Results_LogOut_Log_UTF8_5Col_TQ_UK.txt"));
            Assert.IsTrue(dfs.IsErrorOutputFileSameAs("Empty.txt"));
        }

        [TestMethod]
        public void LogOut_Unicode5ColTQNumericUKInput_FilesMatch()
        {
            DFSIntegrationTestPackage dfs = new DFSIntegrationTestPackage("Input_Err_Unicode_5Col_TQ_UK.txt", _testContextInstance);
            ColumnProperty col = dfs.CreateCol("HEADER1", 0, 0, RunWrapper.DataType.DT_WSTR, 255, 0);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER2", 0, 0, RunWrapper.DataType.DT_UI1, 0, 0);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER3", 0, 0, RunWrapper.DataType.DT_I4, 0, 0);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER4", 12, 6, RunWrapper.DataType.DT_NUMERIC, 0, 0);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            col = dfs.CreateCol("HEADER5", 0, 0, RunWrapper.DataType.DT_DBTIMESTAMP, 0, 0);
            col.TruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);
            dfs.AdvanceSettings.Columns.Add(col);
            dfs.GeneralSettings.Locale = PresenterUtils.ConvertLocaleIDToString(2057);
            dfs.GeneralSettings.TextQualifier = "\"";
            dfs.GeneralSettings.IsUnicode = true;
            dfs.GeneralSettings.ColumnNamesInFirstDataRow = true;
            dfs.AuditingSettings.ErrorSourceDataType = Enum.GetName(typeof(RunWrapper.DataType), RunWrapper.DataType.DT_WSTR);

            //dfs.SavePackageToFile(@"C:\SSIS_TestProject\Integration Services Project1\Integration Services Project1\intdfs.dtsx");

            dfs.Execute();
            Assert.IsTrue(dfs.IsStandardOutputFileSameAs("Results_StdOut_Log_Unicode_5Col_TQ_UK.txt"));
            Assert.IsTrue(dfs.IsLogOutputFileSameAs("Results_LogOut_Log_Unicode_5Col_TQ_UK.txt"));
            Assert.IsTrue(dfs.IsErrorOutputFileSameAs("Results_ErrOut_Log_Unicode_5Col_TQ_UK.txt"));
        }

        [TestMethod]
        public void LogOut_ASCII5ColNoTQMissingColumnsInput_FilesMatch()
        {
            DFSIntegrationTestPackage dfs = new DFSIntegrationTestPackage("Input_Err_ASCII_5Col_NoTQ_MissingColumns.txt", _testContextInstance);
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER1", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER2", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER3", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER4", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER5", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.GeneralSettings.Locale = PresenterUtils.ConvertLocaleIDToString(2057);
            dfs.GeneralSettings.ColumnNamesInFirstDataRow = true;
            dfs.ErrorSettings.ComponentTruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);

            dfs.Execute();
            Assert.IsTrue(dfs.IsStandardOutputFileSameAs("Results_StdOut_Log_ASCII_5Col_NoTQ_MissingColumns.txt"));
            Assert.IsTrue(dfs.IsLogOutputFileSameAs("Results_LogOut_Log_ASCII_5Col_NoTQ_MissingColumns.txt"));
            Assert.IsTrue(dfs.IsErrorOutputFileSameAs("Empty.txt"));
        }

        [TestMethod]
        public void LogOut_ASCII5ColNoTQExtraColumnsInput_FilesMatch()
        {
            DFSIntegrationTestPackage dfs = new DFSIntegrationTestPackage("Input_Err_ASCII_5Col_NoTQ_ExtraColumns.txt", _testContextInstance);
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER1", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER2", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER3", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER4", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.AdvanceSettings.Columns.Add(dfs.CreateCol("HEADER5", 0, 0, RunWrapper.DataType.DT_STR, 255, 1252));
            dfs.GeneralSettings.Locale = PresenterUtils.ConvertLocaleIDToString(2057);
            dfs.GeneralSettings.ColumnNamesInFirstDataRow = true;
            dfs.ErrorSettings.ComponentTruncationHandling = PresenterUtils.ConvertDispositionToString(DTSRowDisposition.RD_IgnoreFailure);

            dfs.Execute();
            Assert.IsTrue(dfs.IsStandardOutputFileSameAs("Results_StdOut_Log_ASCII_5Col_NoTQ_ExtraColumns.txt"));
            Assert.IsTrue(dfs.IsLogOutputFileSameAs("Results_LogOut_Log_ASCII_5Col_NoTQ_ExtraColumns.txt"));
            Assert.IsTrue(dfs.IsErrorOutputFileSameAs("Empty.txt"));
        }
    }
}
